package com.zd.building.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.zd.building.BuildingVo;
import com.zd.building.domain.Building;
import com.zd.building.mapper.BuildingMapper;
import com.zd.building.service.BuildingService;
import com.zd.room.domain.Room;
import com.zd.room.service.RoomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 楼宇Service业务层处理
 * 
 * @author fxiao
 * @date 2020-08-12
 */
@Service
public class BuildingServiceImpl implements BuildingService
{
    private final Logger log= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private RoomService roomService;


    /**
     * 查询楼宇
     * 
     * @param id 楼宇ID
     * @return 楼宇
     */
    @Override
    public BuildingVo selectBuildingById(String id)
    {
        return buildingMapper.selectBuildingById(id);
    }

    /**
     * 查询楼宇列表
     * 
     * @param building 楼宇
     * @return 楼宇
     */
    @Override
    public List<BuildingVo> selectBuildingList(Building building)
    {
        return buildingMapper.selectBuildingList(building);
    }

    @Override
    public List<String> selectCsmcList(Building building) {
        return buildingMapper.selectCsmcList(building);
    }

    @Override
    public Integer checkUnique(Building building) {
        Integer resultNum=buildingMapper.checkUnique(building);
        return resultNum==0?resultNum:1;
    }

    /**
     * 新增楼宇
     * 
     * @param building 楼宇
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBuilding(Building building)
    {
        SysUser sysUser= ShiroUtils.getSysUser();
        building.setId(UUID.randomUUID().toString().replaceAll("-",""));
        building.setCreateBy(sysUser.getLoginName());
        if(building.getDys()==null||building.getDys()==0){
            building.setDys(1);
        }
        //当前栋楼的房间总数
        int totalRoom=building.getDys()*building.getHs()*building.getCs();
        List<Room>roomList=new ArrayList<>();
        for (int j = 0; j < totalRoom; j++) {
            Room room=new Room();
            room.setId(UUID.randomUUID().toString().replaceAll("-",""));
            String roomCodeStr=
                    String.valueOf(j/(building.getHs()*building.getDys())+1)+
                            String.format("%02d",j%(building.getHs()*building.getDys())+1);
            room.setRoomCode(Integer.valueOf(roomCodeStr));
            room.setSsjzId(building.getId());
            roomList.add(room);
        }
        int buildingAddRow=buildingMapper.insertBuilding(building);
        int roomAddRow=roomService.insertRoomList(roomList);
        log.info("本次共保存了{}栋建筑，{}个房间",buildingAddRow,roomAddRow);
        return buildingAddRow;
    }

    @Transactional
    @Override
    public int insertBuildingList(List<Building> buildingList) {
        Integer dys=0;
        SysUser sysUser= ShiroUtils.getSysUser();
        List<Room>roomList=new ArrayList<>();
        for (int i = 0; i < buildingList.size(); i++) {
            //当前栋楼的房间总数
            int totalRoom=0;
            Building currentBuilding=buildingList.get(i);
            currentBuilding.setId(UUID.randomUUID().toString().replaceAll("-",""));
            currentBuilding.setCreateBy(sysUser.getLoginName());
            dys=currentBuilding.getDys();
            if(dys==null||dys==0){
                currentBuilding.setDys(1);
            }
            totalRoom=currentBuilding.getDys()*currentBuilding.getHs()*currentBuilding.getCs();
            for (int j = 0; j < totalRoom; j++) {
                Room room=new Room();
                room.setId(UUID.randomUUID().toString().replaceAll("-",""));
                String roomCodeStr=
                        String.valueOf(j/(currentBuilding.getHs()*currentBuilding.getDys())+1)+
                                String.format("%02d",j%(currentBuilding.getHs()*currentBuilding.getDys())+1);
                room.setRoomCode(Integer.valueOf(roomCodeStr));
                room.setSsjzId(currentBuilding.getId());
                roomList.add(room);
            }
        }
        int updateBuildingRow=buildingMapper.insertBuildingList(buildingList);
        int updateRoomRow=roomService.insertRoomList(roomList);
        log.info("本次共保存了{}栋建筑，{}个房间",updateBuildingRow,updateRoomRow);
        return updateBuildingRow;
    }

    /**
     * 修改楼宇
     * 
     * @param building 楼宇
     * @return 结果
     */
    @Override
    public int updateBuilding(Building building)
    {
        return buildingMapper.updateBuilding(building);
    }

    /**
     * 删除楼宇对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBuildingByIds(String ids)
    {
        int delBuildingRow=0;
        int delRoomRom=0;
        String []idsArr=Convert.toStrArray(ids);
        for (int i = 0; i < idsArr.length; i++) {
            roomService.deleteRoomBySsjzId(idsArr[i]);
            delRoomRom+=1;
        }
        delBuildingRow=buildingMapper.deleteBuildingByIds(idsArr);
        log.info("本次删除了{}栋建筑，{}户房间",delBuildingRow,delRoomRom);
        return delBuildingRow;
    }

    /**
     * 删除楼宇信息
     * 
     * @param id 楼宇ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBuildingById(String id)
    {
        int delBuildingRow=0;
        int delRoomRom=0;
        roomService.deleteRoomBySsjzId(id);
        delBuildingRow=buildingMapper.deleteBuildingById(id);
        delRoomRom=roomService.deleteRoomBySsjzId(id);
        log.info("本次删除了{}栋建筑，{}户房间",delBuildingRow,delRoomRom);
        return delBuildingRow;
    }
}
